﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace _3.WinformFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "请至少选择两个txt文件\n";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            
            ofd.InitialDirectory = Application.StartupPath;
            
            ofd.Title = "请选择要打开的文件";
       
            ofd.Multiselect = true;
            
            ofd.Filter = "文本文件|*.txt";
            
            ofd.FilterIndex = 2;
            
            ofd.RestoreDirectory = true;
            
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                
                string[] filePaths = ofd.FileNames;
                string[] fileNames = ofd.SafeFileNames;
                if (filePaths.Length < 2)
                {
                    label1.Text += "只能至少选择两个txt文件\n";
                    return;
                }

                try
                {
                    using (FileStream fsWrite = new FileStream("Data\\merge.txt", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(fsWrite))
                        {
                            for (int i = 0; i < filePaths.Length; ++i)
                            {
                                string filePath = filePaths[i];
                                using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                                {
                                    using (StreamReader reader = new StreamReader(fsRead))
                                    {
                                        string s = null;
                                        while ((s = reader.ReadLine()) != null)
                                        {
                                            writer.WriteLine(s);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    string mess = "成功将";
                    foreach (string fileName in fileNames)
                    {
                        mess += fileName + " ";
                    }
                    mess += "合并到merge.txt\n";
                    label1.Text += mess;
                }
                catch (Exception ex)
                {
                    label1.Text += ex.Message.ToString();
                    label1.Text += "\n";
                    label1.Text += "合并失败";
                }
            }


        }

    }
}
